///*
// * Copyright (c) 2021-2031, 河北计全科技有限公司 (https://www.jeequan.com & jeequan@126.com).
// * <p>
// * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// * <p>
// * http://www.gnu.org/licenses/lgpl.html
// * <p>
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//package com.expression.sercurity;
//
//
//import com.expression.common.utils.RedisUtils;
//import io.jsonwebtoken.Claims;
//import io.jsonwebtoken.Jws;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
//public class LIAuthenticationTokenFilter extends OncePerRequestFilter {
//
//    private RedisUtils redisUtils;
//    private JwtUtils jwtUtils;
//
//    public LIAuthenticationTokenFilter(RedisUtils redisUtils, JwtUtils jwtUtils) {
//        this.redisUtils = redisUtils;
//        this.jwtUtils = jwtUtils;
//    }
//
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
//
//        LIUserDetails LIUserDetails = commonFilter(request);
//
//        if (LIUserDetails == null) {
//            chain.doFilter(request, response);
//            return;
//        }
//
//        /**
//         * TODO 未完成权限获取
//         */
//
//        //将信息放置到Spring-security context中
//        UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(LIUserDetails, null, LIUserDetails.getAuthorities());
//        authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
//        SecurityContextHolder.getContext().setAuthentication(authentication);
//        chain.doFilter(request, response);
//    }
//
//
//    private LIUserDetails commonFilter(HttpServletRequest request) {
//
//
//        String authToken = request.getHeader(AppConstant.TOKEN);
////        if(StringUtils.isEmpty(authToken)){
////            authToken = request.getParameter(APP.TOKEN);
////        }
//        if (StringUtils.isEmpty(authToken)) {
//            return null; //放行,并交给UsernamePasswordAuthenticationFilter进行验证,返回公共错误信息.
//        }
//
//        //  解析jwt字符串
//        Jws<Claims> claimsJws = jwtUtils.parseToken(authToken);
//        //token字符串解析失败
//        if (claimsJws == null || StringUtils.isEmpty(claimsJws.getBody().getSubject())) {
//            return null;
//        }
//
//        //根据用户名查找数据库
//        LIUserDetails jwtBaseUser = redisUtils.get(AppConstant.USER_DETAIL_PRE+claimsJws.getBody().getSubject(), LIUserDetails.class);
//        if (jwtBaseUser == null) {
//            redisUtils.delete(AppConstant.USER_DETAIL_PRE+claimsJws.getBody().getSubject());
//            return null; //数据库查询失败，删除redis
//        }
//        //续签时间
//        redisUtils.expire(AppConstant.USER_DETAIL_PRE+claimsJws.getBody().getSubject(), AppConstant.EXPIRE_TIME);
//
//        return jwtBaseUser;
//    }
//}
